/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysOrganizationalUnit;
import cn.ibizlab.core.ad.service.SysOrganizationalUnitService;
import cn.ibizlab.core.ad.filter.SysOrganizationalUnitSearchContext;

/**
 * 实体[SysOrganizationalUnit] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysOrganizationalUnitResource {

    @Autowired
    public SysOrganizationalUnitService sysOrganizationalUnitService;

    @Autowired
    @Lazy
    public SysOrganizationalUnitDTOMapping sysOrganizationalUnitDtoMapping;

    @Autowired
    @Lazy
    public SysOrganizationalUnitFilterDTOMapping sysOrganizationalUnitFilterDtoMapping;


    /**
    * 创建Create 组织单元
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalUnitDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"组织单元" },  notes = "SysOrganizationalUnit-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Create-all')")
    @PostMapping(value = {"sys_organizational_units","sys_organizational_units"})
    public ResponseEntity<SysOrganizationalUnitDTO> create
            (@Validated @RequestBody SysOrganizationalUnitDTO dto) {
        SysOrganizationalUnit domain = sysOrganizationalUnitDtoMapping.toDomain(dto);
        sysOrganizationalUnitService.create(domain);
        SysOrganizationalUnit rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 组织单元
    * 
    *
    * @param id id
    * @return ResponseEntity<SysOrganizationalUnitDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"组织单元" },  notes = "SysOrganizationalUnit-Get ")
    @GetMapping("sys_organizational_units/{id}")
    public ResponseEntity<SysOrganizationalUnitDTO> getById
            (@PathVariable("id") String id) {
        SysOrganizationalUnit rt = sysOrganizationalUnitService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 组织单元
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"组织单元" },  notes = "SysOrganizationalUnit-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Remove-all')")
    @DeleteMapping("sys_organizational_units/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysOrganizationalUnitService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 组织单元
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalUnitDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"组织单元" },  notes = "SysOrganizationalUnit-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Update-all')")
    @PutMapping("sys_organizational_units/{id}")
    public ResponseEntity<SysOrganizationalUnitDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationalUnitDTO dto) {
        SysOrganizationalUnit domain = sysOrganizationalUnitDtoMapping.toDomain(dto);
        domain.setId(id);
        sysOrganizationalUnitService.update(domain);
        SysOrganizationalUnit rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 组织单元
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"组织单元" },  notes = "SysOrganizationalUnit-CheckKey ")
    @PostMapping("sys_organizational_units/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysOrganizationalUnitDTO dto) {
        SysOrganizationalUnit domain = sysOrganizationalUnitDtoMapping.toDomain(dto);
        Integer rt = sysOrganizationalUnitService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 组织单元
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalUnitDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"组织单元" },  notes = "SysOrganizationalUnit-GetDraft ")
    @GetMapping("sys_organizational_units/getdraft")
    public ResponseEntity<SysOrganizationalUnitDTO> getDraft
            (@SpringQueryMap SysOrganizationalUnitDTO dto) {
        SysOrganizationalUnit domain = sysOrganizationalUnitDtoMapping.toDomain(dto);
        SysOrganizationalUnit rt = sysOrganizationalUnitService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitDtoMapping.toDto(rt));
    }

    /**
    * Items 组织单元
    * 根据传入的id 查询 id对应单位OU清单，不传入id时自动取当前用户可见OU清单，其余可选过滤参数:filter
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationalUnitDTO>>
    */
    @ApiOperation(value = "Items", tags = {"组织单元" },  notes = "SysOrganizationalUnit-Items 根据传入的id 查询 id对应单位OU清单，不传入id时自动取当前用户可见OU清单，其余可选过滤参数:filter")
    @PostMapping("sys_organizational_units/{id}/items")
    public ResponseEntity<List<SysOrganizationalUnitDTO>> itemsById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationalUnitDTO dto) {
        SysOrganizationalUnit domain = sysOrganizationalUnitDtoMapping.toDomain(dto);
        domain.setId(id);
        List<SysOrganizationalUnit> rt = sysOrganizationalUnitService.items(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 组织单元
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalUnitDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"组织单元" },  notes = "SysOrganizationalUnit-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Save-all')")
    @PostMapping("sys_organizational_units/save")
    public ResponseEntity<SysOrganizationalUnitDTO> save
            (@Validated @RequestBody SysOrganizationalUnitDTO dto) {
        SysOrganizationalUnit domain = sysOrganizationalUnitDtoMapping.toDomain(dto);
        sysOrganizationalUnitService.save(domain);
        SysOrganizationalUnit rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 组织单元
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationalUnitDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"组织单元" },  notes = "SysOrganizationalUnit-FetchDefault ")
    @PostMapping("sys_organizational_units/fetchdefault")
    public ResponseEntity<List<SysOrganizationalUnitDTO>> fetchDefault
            (@Validated @RequestBody SysOrganizationalUnitFilterDTO dto) {
        SysOrganizationalUnitSearchContext context = sysOrganizationalUnitFilterDtoMapping.toDomain(dto);
        Page<SysOrganizationalUnit> domains = sysOrganizationalUnitService.searchDefault(context) ;
        List<SysOrganizationalUnitDTO> list = sysOrganizationalUnitDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchMyOrg 组织单元
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationalUnitDTO>>
    */
    @ApiOperation(value = "查询FetchMyOrg", tags = {"组织单元" },  notes = "SysOrganizationalUnit-FetchMyOrg ")
    @PostMapping("sys_organizational_units/fetchmyorg")
    public ResponseEntity<List<SysOrganizationalUnitDTO>> fetchMyOrg
            (@Validated @RequestBody SysOrganizationalUnitFilterDTO dto) {
        SysOrganizationalUnitSearchContext context = sysOrganizationalUnitFilterDtoMapping.toDomain(dto);
        Page<SysOrganizationalUnit> domains = sysOrganizationalUnitService.searchMyOrg(context) ;
        List<SysOrganizationalUnitDTO> list = sysOrganizationalUnitDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建组织单元
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Create-all')")
    @ApiOperation(value = "批量新建组织单元", tags = {"组织单元" },  notes = "批量新建组织单元")
	@PostMapping("sys_organizational_units/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysOrganizationalUnitDTO> dtos) {
        sysOrganizationalUnitService.createBatch(sysOrganizationalUnitDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除组织单元
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Remove-all')")
    @ApiOperation(value = "批量删除组织单元", tags = {"组织单元" },  notes = "批量删除组织单元")
	@DeleteMapping("sys_organizational_units/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysOrganizationalUnitService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新组织单元
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Update-all')")
    @ApiOperation(value = "批量更新组织单元", tags = {"组织单元" },  notes = "批量更新组织单元")
	@PutMapping("sys_organizational_units/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysOrganizationalUnitDTO> dtos) {
        sysOrganizationalUnitService.updateBatch(sysOrganizationalUnitDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存组织单元
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Save-all')")
    @ApiOperation(value = "批量保存组织单元", tags = {"组织单元" },  notes = "批量保存组织单元")
	@PostMapping("sys_organizational_units/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysOrganizationalUnitDTO> dtos) {
        sysOrganizationalUnitService.saveBatch(sysOrganizationalUnitDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入组织单元
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalUnit-Save-all')")
    @ApiOperation(value = "批量导入组织单元", tags = {"组织单元" },  notes = "批量导入组织单元")
	@PostMapping("sys_organizational_units/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysOrganizationalUnitDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalUnitService.importData(config,ignoreError,sysOrganizationalUnitDtoMapping.toDomain(dtos)));
    }

}
